<?php

class ProblemController extends CSupervisorController {

    public $pageTitle = "Soal";
    private $_model;

    public function actionIndex() {
        /*$problems = Problem::model()->findAll();

        foreach ($problems as $p) {
            $dir =  $p->getDirectoryPath() . 'evaluator/files';
            //echo $dir . "<br />";
            echo exec("dos2unix $dir/*");
        }*/

        $criteria = new CDbCriteria;
        $filter = 'all';
        if (isset($_GET['filter'])) {
            $filter = $_GET['filter'];
            switch ($filter) {
                case 'mine' :
                    $criteria->addCondition('author_id = ' . Yii::app()->user->getId());
                    break;
                case 'all' :
                default:
                    break;
            }
        }
        if (isset($_GET['search'])) {
            $search = $_GET['search'];
            $criteria->addSearchCondition('title', $search);
            $criteria->addSearchCondition('description', $search, true, 'OR');
        }
        //NOTE: Optimize SQL
        $criteria->with = array('author' => array('select' => 'id, full_name'));
        $dataProvider = new CActiveDataProvider('Problem', array(
                    'pagination' => array(
                        'pageSize' => 20,
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('index', array('dataProvider' => $dataProvider));
    }

    public function actionCreate() {
        $model = new Problem('create');
        if (isset($_POST['Problem'])) {
            $model->attributes = $_POST['Problem'];
            $model->visibility = Problem::VISIBILITY_DRAFT;
            if ($model->validate()) {
                $model->author_id = Yii::app()->user->getId();
                $model->save(false);
                $this->redirect(array('update', 'id' => $model->id));
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionDelete() {
        $model = $this->loadModel();
        $model->delete();
        if (!isset($_GET['ajax']))
            $this->redirect(array('index'));
    }

    public function actionUpdate() {
        $model = $this->loadModel();
        if (isset($_POST['Problem'])) {
            $model->attributes = $_POST['Problem'];
            if ($model->save()) {
                $this->redirect(array('update', 'id' => $model->id));
            }
        }
        $this->render('update', array('model' => $model));
    }

    public function actionConfigure() {
        $model = $this->loadModel();
        $this->render('configure', array('model' => $model));
    }

    public function actionPublish() {
        $model = $this->loadModel();
        $model->visibility = Problem::VISIBILITY_PUBLIC;
        $model->save();
        if (!isset($_GET['ajax']))
            $this->redirect(array('index'));
    }

    public function actionUnpublish() {
        $model = $this->loadModel();
        $model->visibility = Problem::VISIBILITY_DRAFT;
        $model->save();
        if (!isset($_GET['ajax']))
            $this->redirect(array('index'));
    }

    public function actionRegrade() {
        $model = $this->loadModel();
        if (isset($_GET['submissionid']))
            $submission = Submission::model()->findbyPk($_GET['submissionid']);
        //
        if ($submission !== null) {
            $submission->setGradeStatus(Submission::GRADE_STATUS_PENDING);
            $submission->save(false);
            $this->redirect(array('submissions', 'id' => $model->id));
        } else {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
    }

    //
    public function actionView() {
        $model = $this->loadModel();
        $this->render('view', array('model' => $model));
    }

    public function actionDownload() {
        $model = $this->loadModel();
        $id = $model->id;
        $tempnam = tempnam("/tmp", "p$id");
        if (Zip::createZip($model->getDirectoryPath(), $tempnam)) {
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="' . $id . '.zip"');
            readfile($tempnam);
            unlink($tempnam);
        }
        exit();
    }

    public function actionSubmissions() {
        $model = $this->loadModel();
        $submissionDataProvider = new CActiveDataProvider('Submission', array(
                    'pagination' => array(
                        'pageSize' => 20,
                    ),
                    'criteria' => array(
                        'select' => array('id', 'submitted_time', 'grade_status', 'score', 'verdict'),
                        'condition' => 'contest_id IS NULL AND problem_id = :problem_id',
                        'params' => array(
                            'problem_id' => $model->id
                        ),
                        'with' => array(
                            'problem' => array('select' => array('id', 'title')),
                            'problem.problemtype' => array('select' => array('id', 'name')),
                            'submitter' => array('select' => array('id', 'full_name'))
                        )
                    )
                ));
        $this->render('submissions', array(
            'model' => $model,
            'submissionDataProvider' => $submissionDataProvider,
        ));
    }

    public function actionBatchRegrade() {
        if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
            if (isset($_POST['mark'])) {
                foreach ($_POST['mark'] as $regradeid) {
                    $model = Submission::model()->findbyPk($regradeid);
                    if ($model !== null) {
                        $model->setGradeStatus(Submission::GRADE_STATUS_PENDING);
                        $model->save(false);
                    }
                }
            }
        }
    }

    public function actionBatchSkip() {
        if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
            if (isset($_POST['mark'])) {
                foreach ($_POST['mark'] as $regradeid) {
                    $model = Submission::model()->findbyPk($regradeid);
                    if ($model !== null) {
                        $model->setGradeStatus(Submission::GRADE_STATUS_NOGRADE);
                        $model->save(false);
                    }
                }
            }
        }
    }

    //

    public function loadModel() {
        if ($this->_model === null) {
            if (isset($_GET['id']))
                $this->_model = Problem::model()->findbyPk($_GET['id']);
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

}
